<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hizlialsat_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
        
    public function get_products()
    {
        $this->db->select('*');
        $this->db->from('products');
        $this->db->join('seller','seller.product_id = products.product_id');
        $this->db->where(array('seller.mod' => 1,'seller.status' => 1));
        $result = $this->db->get();
        return  $result;
        
    }    
    public function get_user_talepler($user_id,$talep_id = NULL)
    {
        $where = "products.product_id = seller.product_id AND seller.mod = 2 AND talepler.seller_id = seller.seller_id AND talepler.talep_user_id = seller.user_id";
        $this->db->select('*');
        $this->db->from('products');
        $this->db->from('seller');
        $this->db->from('talepler');
        //$this->db->join('seller','seller.product_id = products.product_id');
        //$this->db->join('talepler','talepler.talep_user_id = seller.user_id');
        $this->db->where($where);
         if($talep_id != NULL)
        {
            $this->db->where('talepler.talep_id',$talep_id);
        }
        $this->db->where('talepler.talep_user_id',$user_id);
        $this->db->where('talepler.status',1);
        $this->db->where('seller.status',1);
        
        $result = $this->db->get();
        //$sql = "SELECT * FROM products,seller,talepler WHERE products.product_id = seller.product_id AND seller.mod = 2 AND talepler.seller_id = seller.seller_id AND talepler.talep_user_id = seller.user_id";
        //$result = $this->db->query($sql)->result();
        return  $result; 
    }    
    public function get_all_talepler($user_id,$talep_id = NULL)
    {
        $where = "products.product_id = seller.product_id AND seller.mod = 2 AND talepler.seller_id = seller.seller_id AND talepler.talep_user_id = seller.user_id ";
        $this->db->select('*');
        $this->db->from('products');
        $this->db->from('seller');
        $this->db->from('talepler');
  //      $this->db->group_by('talepler.product_id');
        
        if($talep_id != NULL)
        {
            $this->db->where('talepler.talep_id',$talep_id);
        }
        //$this->db->join('seller','seller.product_id = products.product_id');
        //$this->db->join('talepler','talepler.talep_user_id = seller.user_id');
        $this->db->where($where);
        $this->db->where('talepler.talep_user_id <>',$user_id);
        $this->db->where('talepler.status',1);
        $this->db->where('seller.status',1);
        $result = $this->db->get();
        //$sql = "SELECT * FROM products,seller,talepler WHERE products.product_id = seller.product_id AND seller.mod = 2 AND talepler.seller_id = seller.seller_id AND talepler.talep_user_id = seller.user_id";
        //$result = $this->db->query($sql)->result();
        return  $result; 
    }     
    public function get_all_talepler_limit($user_id,$talep_id = NULL)
    {
        $where = "products.product_id = seller.product_id AND seller.mod = 2 AND talepler.seller_id = seller.seller_id AND talepler.talep_user_id = seller.user_id";
        $this->db->select('*');
        $this->db->from('products');
        $this->db->from('seller');
        $this->db->from('talepler');
  //      $this->db->group_by('talepler.product_id');
        
        if($talep_id != NULL)
        {
            $this->db->where('talepler.talep_id',$talep_id);
        }
        //$this->db->join('seller','seller.product_id = products.product_id');
        //$this->db->join('talepler','talepler.talep_user_id = seller.user_id');
        $this->db->where($where);
        $this->db->where('talepler.talep_user_id <>',$user_id);
        $this->db->where('talepler.status',1);
        $this->db->where('seller.status',1);
        $this->db->limit(20,$this->uri->segment(3));        
        $result = $this->db->get();
        //$sql = "SELECT * FROM products,seller,talepler WHERE products.product_id = seller.product_id AND seller.mod = 2 AND talepler.seller_id = seller.seller_id AND talepler.talep_user_id = seller.user_id";
        //$result = $this->db->query($sql)->result();
        return  $result; 
    }   
    function get_hizlial_products($limit, $offset, $sort_by, $sort_order,$num_rows = 0) {
		
     
	    $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('product_id', 'name', 'model', 'user_price', 'stock', 'brand_name','dateadd');
		$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'model';
        
	
        if($num_rows == 0)
        {
             $sql = "SELECT * FROM products
               JOIN seller ON seller.product_id = products.product_id 
               WHERE
               seller.product_id = products.product_id AND
               seller.mod = 1 and seller.status = 1
               ORDER BY $sort_by $sort_order               
               LIMIT $offset, $limit ";
                  
           return $this->db->query($sql);
        }
        else
        {
        $sql = "SELECT * FROM products
               JOIN seller ON seller.product_id = products.product_id 
               WHERE
               seller.product_id = products.product_id AND
               seller.mod = 1  and seller.status = 1
               ORDER BY $sort_by $sort_order               
               ";
                  
           return $this->db->query($sql); 
        }
        
         
		// results query

	}      
 
        
        

}
?>